﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;

namespace Sispe.Repository
{
    public class VinculoFamiliarRepository:MasterRepository,IVinculoFamiliarRepository
    {
        public List<VINCULO_FAMILIAR> GetAllFromVinculoFamiliar()
        {
            var query = from vf in context.VINCULO_FAMILIAR
                        select vf;
            return query.ToList();
        }

        public List<VINCULO_FAMILIAR> GetAllFromVinculoFamiliarByDescripcion(string descripcion)
        {
            var query = from vf in context.VINCULO_FAMILIAR
                        select vf;
            if (descripcion != null && descripcion != string.Empty && descripcion != "")
            {
                query = from vf in context.VINCULO_FAMILIAR
                        where vf.Descripcion.ToUpper().Contains(descripcion) ||
                                   vf.Codigo.Contains(descripcion)
                        select vf;
            }
            return query.ToList();
        }

        public VINCULO_FAMILIAR GetFromVinculoFamiliarById(int id)
        {
            var query = from vf in context.VINCULO_FAMILIAR
                        where vf.IdVinculoFamiliar.Equals(id)
                        select vf;
            return query.SingleOrDefault();
        }

        public VINCULO_FAMILIAR GetFromVinculoFamiliarByCodigo(string codigo)
        {
            var query = from vf in context.VINCULO_FAMILIAR
                        where vf.Codigo.Equals(codigo)
                        select vf;
            return query.SingleOrDefault();
        }

        public bool ExisteDuplicidad(string descripcion, string codigo,Int32 id)
        {
            var query = from vf in context.VINCULO_FAMILIAR
                        where (vf.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0 ||
                                   vf.Codigo.CompareTo(codigo) == 0) && vf.IdVinculoFamiliar!=id
                        select vf;
            return query.Any();
        }

        public void AddVinculoFamiliar(VINCULO_FAMILIAR vinculoFamiliar)
        {
            context.VINCULO_FAMILIAR.Add(vinculoFamiliar);
            context.SaveChanges();
        }

        public void UpdateVinculoFamiliar(VINCULO_FAMILIAR vinculoFamiliar)
        {
            context.Entry(vinculoFamiliar).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void RemoveVinculoFamiliar(int id)
        {
            var existe = context.VINCULO_FAMILIAR.Find(id);

            if (existe != null)
            {
                context.VINCULO_FAMILIAR.Remove(existe);
                context.SaveChanges();
            }
        }
    }
}
